﻿$(function(){
    console.log(_experts);
    console.log(_departs);
    var departmodel = $(".departmodel").clone();
 
    if(_departs){
        for(var i=0;i<_departs.length;i++){
            var departId = _departs[i].departId;
            var departName = _departs[i].departName;
            var onetotal = _departs[i].onetotal;
            var onenum = _departs[i].onenum;
            var onepercent = Math.round(onenum * 100 / onetotal);
            var twototal = _departs[i].twototal;
            var twonum = _departs[i].twonum;
            var twopercent = Math.round(twonum * 100 / twototal);
            var onerow = departmodel.clone();
            onerow.attr("departId", departId);
            onerow.children().eq(1).html("<a href='../ZP/SelfExaminationView.aspx?DepartID={0}'>{1}</a><br />基本：{2}/{3}({4}%)<br />测评：{5}/{6}({7}%)".format(departId, departName, onenum, onetotal, onepercent, twonum, twototal, twopercent));//部门名称
            
           var experts = "";
            if(_experts){
                for(var j=0;j<_experts.length;j++){
                    experts += "<input type='checkbox' name='exp_{0}' id='exp_{0}' value='{2}'/>&nbsp;<label for='exp_{0}'>{1}</label>&nbsp;&nbsp;".format(i + "_" + j, _experts[j].exTrueName, departId + "_" + _experts[j].exId);
                }
            }
            console.log(i);
            onerow.children().eq(3).html(experts);
            $(".departmodel").last().after(onerow);
        }
        $(".departmodel").each(function(n){
            if(n > 0){
                $(this).removeClass("departmodel");
            }
        });
        $("#departNum").val(_departs.length);
        if(_experts){
             $("#expertNum").val(_experts.length);
        }
    }
    
    if(_expertDepart){
        for(var i=0;i<_expertDepart.length;i++){
            var expertId = _expertDepart[i].expertId;
            var departId = _expertDepart[i].departId;
            var hasMark = _expertDepart[i].hasMark.toLowerCase();
            $("tr[departid='{0}'] input[value='{0}_{1}']".format(departId, expertId)).attr("checked", true);
            if("true" == hasMark){
                $("tr[departid='{0}'] input[value='{0}_{1}']".format(departId, expertId)).next().each(function(){
                    $(this).html($(this).html() + "*");
                    $(this).attr("title","该专家已为该部门打过分，如果取消，则相应分数会删除！（谨慎）");
                });
            }
        }
    }
    
    
    $(":checkbox").each(function(){
    
        if($(this).attr("checked")){
            $(this).click(function(){
                var id = "{0}_delete".format($(this).attr("id"));
                if(!$(this).attr("checked")){
                     if($("#"+id).size() < 1){
                            $("<input type='hidden' id='{0}' name='{0}' value='true'/>".format(id)).appendTo("form");
                     }
                } else {
                    $("#"+id).remove();
                }            
            });
        } else {
            $(this).click(function(){
                var id = "{0}_save".format($(this).attr("id"));
                if($(this).attr("checked")){
                    if($("#"+id).size() < 1){
                        $("<input type='hidden' id='{0}' name='{0}' value='true'/>".format(id)).appendTo("form");
                    }
                } else {
                    $("#"+id).remove();
                }
            });
        }
    });
    
});

